﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class LoaiDocGiaDAO
    {
        #region 1. Them
        public static bool ThemLoaiDG(LoaiDocGiaDTO loaidg){
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaLoaiDG", loaidg.MaLoaiDG));
                sqlParams.Add(new SqlParameter("@TenLoaiDG", loaidg.TenLoaiDG));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_InsertLOAIDOCGIum", sqlParams);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        #endregion
        #region 2. Sua
        public static bool SuaLoaiDG(LoaiDocGiaDTO loaidg)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaLoaiDG", loaidg.MaLoaiDG));
                sqlParams.Add(new SqlParameter("@TenLoaiDG", loaidg.TenLoaiDG));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_UpdateLOAIDOCGIum", sqlParams);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 3. Xoa
        public static bool XoaLoaiDG(String maloaidg)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaLoaiDG", maloaidg));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_DeleteLOAIDOCGIum", sqlParams);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 4. Tim kiem
        public static LoaiDocGiaDTO TimLoaiDocGia(String maloaidg){
            LoaiDocGiaDTO result = new LoaiDocGiaDTO();
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaLoaiDG", maloaidg));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectLOAIDOCGIum", sqlParams);
                DataRow dr = dt.Rows[0];

                result.MaLoaiDG = dr["MaLoaiDG"].ToString();
                result.TenLoaiDG = dr["TenLoaiDG"].ToString();
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        public static List<LoaiDocGiaDTO> TimTatCaLoaiDG(){
            List<LoaiDocGiaDTO> result = new List<LoaiDocGiaDTO>();
            try
            {
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectLOAIDOCGIAsAll");
                foreach (DataRow dr in dt.Rows)
                {
                    LoaiDocGiaDTO tmp = new LoaiDocGiaDTO();
                    tmp.MaLoaiDG = dr["MaLoaiDG"].ToString();
                    tmp.TenLoaiDG = dr["TenLoaiDG"].ToString();

                    result.Add(tmp);
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        public static bool CheckLoaiDGByID(String maloaidg){
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaLoaiDG", maloaidg));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectLOAIDOCGIum", sqlParams);
                if (dt.Rows.Count > 0)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        #endregion
    }
}
